package org.wuxinshui.boosters.designPatterns.strategy.demo3;

import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

public class BaseContext<O, T extends BaseHandler> {


    @Autowired(required = false)
    private List<T> handlers = new ArrayList<>();

    private T defaultHandler;

    @PostConstruct
    public void init() {
        for (T handler : handlers) {
            init(handler);
            if (handler.isDefault()) {
                defaultHandler = handler;
                break;
            }
        }
    }


    public void init(T t) {

    }

    /**
     * 处理器选择
     *
     * @param support
     * @return
     */
    public T getHandler(O support) {
        return handlers.stream().filter(handler -> handler.support(support) && !handler.isDefault()).findAny().orElse(defaultHandler);
    }

}
